package com.danskin.wechat.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.danskin.wechat.entity.CustOrderEvaluate;
import com.danskin.wechat.service.CustOrderEvaluateService;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.Page;

@Controller
@RequestMapping("order")
public class CustOrderEvaluateController extends BaseController{
	
	@Autowired
	private CustOrderEvaluateService orderEvaluateService;

	private List<Map<String,Object>> list=null;

	@RequestMapping(value = "/orderEvaluate", produces = "application/json;charset=UTF-8")
	public String orderEvaluate(){
		return "orderEvaluate/evaluate_list";
	}

	@RequestMapping(value = "/showOrderEvaluateDetail", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	public String showOrderEvaluateDetail(String id, Model model) throws Exception {
		if (list.size() > 0) {
			for (Map<String, Object> map2 : list) {
				if (id.equals(String.valueOf(map2.get("id")))) {
					model.addAttribute("map", map2);
					break;
				}
			}
		}
		return "orderEvaluate/evaluate_detail";
	}
	
	/***
	 * 导出扫码评价数据
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/exportOrderEvaluate", method = RequestMethod.GET)
	public void exportOrderEvaluate(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{	
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String name = request.getParameter("name");
		String mobile = request.getParameter("mobile");
		String orderNo = request.getParameter("orderNo");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String mpId = String.valueOf(this.getMpId());
		
		List<CustOrderEvaluate> list = orderEvaluateService.exportOrderEvaluate(name,mobile,orderNo,startTime,endTime,mpId);
		HSSFWorkbook wb = new HSSFWorkbook();  
		HSSFSheet sheet = wb.createSheet("扫码评价数据统计");
		HSSFRow row = sheet.createRow((int) 0);  
		HSSFCellStyle style = wb.createCellStyle();    
        //style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 
        //设置标题字体加粗
        HSSFFont font = wb.createFont(); 
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);// 设置字体大小
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
        style.setFont(font);
        
        HSSFCell cell = row.createCell((short) 0); 
        cell.setCellValue("订单编号");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 1);    
        cell.setCellValue("店铺编号");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 2);  
        cell.setCellValue("会员卡号");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 3);  
        cell.setCellValue("手机号");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 4);  
        cell.setCellValue("顾客姓名");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 5);
        cell.setCellValue("评价1问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 6);
        cell.setCellValue("评价1分数");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 7);
        cell.setCellValue("评价2问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 8);
        cell.setCellValue("评价2分数");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 9);
        cell.setCellValue("评价3问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 10);
        cell.setCellValue("评价3分数");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 11);
        cell.setCellValue("评价4问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 12);
        cell.setCellValue("评价4回答");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 13);
        cell.setCellValue("评价5问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 14);
        cell.setCellValue("评价5回答");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 15);
        cell.setCellValue("评价6问题");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 16);
        cell.setCellValue("评价6回答");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 17);        
        cell.setCellValue("评价时间");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 18);        
        cell.setCellValue("客服备注");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 19);       
        for (int i = 0; i < list.size(); i++){
        	row = sheet.createRow((int) i + 1);          	
        	CustOrderEvaluate order = list.get(i);
        	row.createCell((short) 0).setCellValue(order.getOrderNo());        	
        	row.createCell((short) 1).setCellValue(order.getStoreCode());  
        	row.createCell((short) 2).setCellValue(order.getCardNo());
        	row.createCell((short) 3).setCellValue(order.getMobile());
        	row.createCell((short) 4).setCellValue(order.getName());
        	row.createCell((short) 5).setCellValue("请对店铺卫生情况进行评价：");
        	row.createCell((short) 6).setCellValue(order.getStoreHealth());
        	row.createCell((short) 7).setCellValue("请对导购的产品特点介绍服务进行评价：");
        	row.createCell((short) 8).setCellValue(order.getProdCharacter());
        	row.createCell((short) 9).setCellValue("请对产品穿用的舒适度进行评价：");
        	row.createCell((short) 10).setCellValue(order.getProdComfort());
        	row.createCell((short) 11).setCellValue("您还有什么要对我们说的？");
        	row.createCell((short) 12).setCellValue(order.getMessage());
        	row.createCell((short) 13).setCellValue("以后您是否愿意再来光临本店？");
        	if(order.getIsComeback()!=null){
        		if(order.getIsComeback()==0){
            		row.createCell((short) 14).setCellValue("不愿意");  
            	}else{
            		row.createCell((short) 14).setCellValue("愿意");  
            	}
        	}else{
        		row.createCell((short) 14).setCellValue("愿意");  
        	}  
        	row.createCell((short) 15).setCellValue("以后您是否愿意继续购买李宁品牌的产品？");
        	if(order.getIsBuyproduct()!=null){
        		if(order.getIsBuyproduct()==0){
        			row.createCell((short) 16).setCellValue("不愿意"); 
        		}else{
        			row.createCell((short) 16).setCellValue("愿意"); 
        		} 
        	}else{
        		row.createCell((short) 16).setCellValue("愿意"); 
        	}
        	if(order.getCreatedTime()!=null){
            	cell = row.createCell((short) 17); 
            	cell.setCellValue(dateFormater.format(order.getCreatedTime()));
            }
        	row.createCell((short) 18).setCellValue(order.getRemark());  
        }
        OutputStream out = null;   
        try {        
            out = response.getOutputStream();    
            String fileName = "扫码评价数据统计.xls";// 文件名        
            response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8"); 
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"), "iso8859-1"));
            wb.write(out);   
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {      
            try {       
                out.close();      
            } catch (IOException e) {      
                e.printStackTrace();    
            }      
        }  
	}

	@RequestMapping(value = "/getOrderEvaluateList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getOrderEvaluateList(Page page) throws Exception {
		Map<String, Object> map = getRequestParameter(this.getRequest());
		map.put("mpId", this.getLoginUser().getMpId());
		page.setMap(map);
		list = orderEvaluateService.listPageGetCustOrderEvaluate(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}
	@RequestMapping(value = "/doUpdateOrderEvaluate", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String doUpdateOrderEvaluate(@RequestBody CustOrderEvaluate evaluate) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		int i = orderEvaluateService.updateByPrimaryKeySelective(evaluate);
		if (i > 0) {
			result.put("status", 200);
			result.put("message", "成功");
		} else {
			result.put("status", 400);
			result.put("message", "失败");
		}

		return JsonUtils.objectToJson(result);
	}
}
